package com.jimikongjian.controller;

import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.models.UnreadComment;
import com.jimikongjian.service.base.IUnreadCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by fuxiuyang on 17/3/15.
 */
@RestController("unreadCommentController")
public class UnreadCommentController {


    @Autowired
    @Qualifier("unreadCommentService")
    private IUnreadCommentService unreadCommentService;

    /**
     * 创建一条未读评论
     * @param unreadComment
     * @return
     */
    @RequestMapping(value = "/unread_comment/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage createUnreadComment(@RequestBody UnreadComment unreadComment){
        return unreadCommentService.createUnreadComment(unreadComment);
    }

    /**
     * 根据用户的id查询该用户的的所有的未读评论
     * @param userId
     * @param start
     * @param length
     * @return
     */
    @RequestMapping(value = "/unread_comment", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectUnreadComment(@RequestParam(name = "userId", defaultValue = "-1") Integer userId,
                                               @RequestParam(name = "start", defaultValue = "0") Integer start,
                                               @RequestParam(name = "length", defaultValue = "10") Integer length){
        return unreadCommentService.selectUnreadCommentByUserId(userId, start, length);
    }

    /**
     * 根据用户的id查询该用户的的所有的未读评论
     * @param map
     * @return
     */
    @RequestMapping(value = "/unread_comment", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseMessage updateComment(@RequestBody Map<String, String> map){
        int id = UnreadComment.INIT_ID;
        if (map.containsKey("id")){
            id = Integer.parseInt(map.get("id"));
        }
        return unreadCommentService.updateUnreadCommentById(id);
    }

    /**
     * 删除用户的未读评论
     * @param unreadComment
     * @return
     */
    @RequestMapping(value = "/unread_comment", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseMessage deleteUnreadComment(@RequestBody UnreadComment unreadComment){
        return unreadCommentService.deleteUnreadComment(unreadComment);
    }
}
